/* 
 * File:   lexeme.cpp
 * Author: ScaV
 * 
 * Created on October 26, 2011, 5:57 PM
 */

#include "lexeme.h"
#include <cstring>

lexeme::lexeme() {
	str = new std::vector<char>;
	type = LEX_UNKNOWN;
	index = NULL;
	line = 0;
}

lexeme::lexeme(const lexeme& orig) {
}

lexeme::~lexeme() {
}

char *lexeme::getType() {
	char *str;
	str = new char[14];
	switch (type) {
		case LEX_UNKNOWN:		strcpy(str, "UNKNOWN      ");	break;
		case LEX_KEYWORD:		strcpy(str, "KEYWORD      ");	break;
		case LEX_CONSTANT:		strcpy(str, "CONSTANT     ");	break;
		case LEX_IDENTIFICATOR:	strcpy(str, "IDENTIFICATOR");	break;
		case LEX_SEPARATOR:		strcpy(str, "SEPARATOR    ");	break;
		default:				throw "Invalid lexType";
	}
	return str;
}

char *lexeme::getName() {
	char *name;
	int i;
	name = new char[str->size() + 1];
	std::vector<char>::iterator iter;
	for (i = 0, iter = str->begin(); iter != str->end(); ++i, ++iter)
		name[i] = *iter;
	name[i] = '\0';
	return name;
}

bool lexeme::setType(LEX_TYPE tp) {
	if (tp > LEX_MAX || tp < 0)
		return false;
	type = tp;
	return true;
}

bool lexeme::setName(std::vector<char>& name) {
	*str = name;
	if (!LEX_ID_LIMIT || type != LEX_IDENTIFICATOR)
		return true;
	for (int i = str->size(); i > LEX_ID_LIMIT ; --i)
		str->pop_back();
	return true;
}

bool lexeme::setLine(int ln) {
	if (line < 0)
		return false;
	line = ln;
	return true;
}

